package Util::News::MyNews;
use Moose;
use namespace::autoclean;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

#####################################################
#Input: 
#	user_id -- user id to search
#	page -- describe which page does my_news return
#Return: 
#	news -- All the news of friends 
#####################################################
sub my_news {
	my ($self, $user_id, $page) = @_;

	my $news;
	unless(defined($page)) {
		$news = $self->schema->resultset('Mynew')->search(
			{
				'users_users.me_id' => $user_id,
				'users_users.status' => 'Y',
			},
			{
				join => ['users_users']
			}
		);
	} else {
		$news = $self->schema->resultset('Mynew')->search(
			{
				'users_users.me_id' => $user_id,
				'users_users.status' => 'Y',
			},
			{
				join => ['users_users'],
				order_by => {-desc => [qw/me.created/]},
				page => $page,
			}
		);
	}

	return $news;
}

__PACKAGE__->meta->make_immutable;

1;
